package com.casstime.ec.cloud.discount.restful;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.casstime.commons.utils.JsonUtil;
import com.casstime.ec.cloud.clients.feign.product.ProductClient;
import com.casstime.ec.cloud.coupon.enums.AvailableChannelEnum;
import com.casstime.ec.cloud.coupon.models.ProductCouponActivity;
import com.casstime.ec.cloud.coupon.param.SupplierProductCouponParam;
import com.casstime.ec.cloud.coupon.param.SupplierProductCouponParam.SupplierProductItem;
import com.casstime.ec.cloud.discount.client.Companyv2Client;
import com.casstime.ec.cloud.discount.client.CouponClient;
import com.casstime.ec.cloud.service.GenericService;
import com.casstime.ec.cloud.service.couponPromotion.CouponPromotionService;
import com.casstime.ec.cloud.user.spi.models.Company;
import com.casstime.ec.cloud.values.amountPromotion.AmountPromotionQueryRequest;
import com.casstime.ec.cloud.values.product.info.ProductFull;
import com.casstime.ec.cloud.values.product.param.ProductIdRequest;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 
 * ClassName: CouponPromotionServiceRest 
 * Function: (新增优惠券促销封装接口)
 * Reason: ADD REASON(可选).
 *  
 * @author hongcheng.wu  
 * 2018年5月3日 下午3:56:17
 * @version   
 * @since JDK 1.8
 */
@RestController
public class CouponPromotionServiceRest extends GenericService implements CouponPromotionService {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ProductClient productClient;
	
	@Autowired
	private Companyv2Client companyClient;
	
	@Autowired
	private CouponClient couponClient;
	
	@Override
	public List<ProductCouponActivity> queryCouponProList(@RequestBody AmountPromotionQueryRequest queryRequest) throws Exception {
		if (queryRequest == null) {
			return Lists.newArrayList();
		}
		if (CollectionUtils.isEmpty(queryRequest.getProductIds())) {
			return Lists.newArrayList();
		}
		List<SupplierProductItem> supplierProductItemList = Lists.newArrayList();
		List<String> productStoreIds = Lists.newArrayList();
		
		SupplierProductCouponParam param = new SupplierProductCouponParam();
		
		List<String> resultColumns = new ArrayList<>();
		resultColumns.add("ORIGINAL_PARTS");
		resultColumns.add("BRAND");
		resultColumns.add("BASE_CATAGORY");
		
		ProductIdRequest preq = new ProductIdRequest();
		preq.setUserLoginId(queryRequest.getUserLoginId());
		preq.setProductIds(queryRequest.getProductIds());
		preq.setResultColumn(resultColumns);
		
		Map<String, ProductFull> productFullMap = Maps.newHashMap();
		try {
			productFullMap = productClient.getProductsByIdsV2(preq);
		} catch (Exception e1) {
			logger.error("查询商品失败 req= {}", JsonUtil.serializer(preq), e1);
		}
		
		for (ProductFull productFull : productFullMap.values()) {
			SupplierProductCouponParam.SupplierProductItem supplierProductItem = param.new SupplierProductItem();
			if (productFull.getBaseCatagory() != null && productFull.getBaseCatagory().getParentItemId() != null) {
				supplierProductItem.setSupplierCompanyId(productFull.getProductInfo().getProductStoreId()); 	//标识
				supplierProductItem.setProductId(productFull.getProductId());									//商品ID
				supplierProductItem.setSequenceId(productFull.getProductId());									//序列ID
				supplierProductItem.setProductBrand(productFull.getBrand().getBrandCode()); 					//商品品牌
				supplierProductItem.setProductCategory(productFull.getBaseCatagory().getParentItemId());		//商品品类
				productStoreIds.add(productFull.getProductInfo().getProductStoreId());
				
				supplierProductItemList.add(supplierProductItem);
			}
		}
		List<Company> companyList = Lists.newArrayList();
		try {
			companyList = companyClient.listCompaniesByCodes(productStoreIds);
		} catch (Exception e) {
			logger.error("查询供应商信息失败 req= {}", JsonUtil.serializer(productStoreIds), e);
		}
		
		if (CollectionUtils.isEmpty(supplierProductItemList)) {
			return Lists.newArrayList();
		}
		
		for (SupplierProductItem supplierProductItem : supplierProductItemList) {
			for (Company company : companyList) {
				if (supplierProductItem.getSupplierCompanyId().equals(company.getProductStoreId())) {
					supplierProductItem.setSupplierCompanyId(company.getCompanyId());
				}
			}
		}
		param.setChannel(AvailableChannelEnum.APP);
		param.setGarageCompanyId(queryRequest.getCompanyId()); 	//优惠券使用方：维修厂公司id
		param.setSupplierProductItems(supplierProductItemList); //各供应商订单列表参数
		List<ProductCouponActivity> productCouponActivityList = Lists.newArrayList();
		try {
			productCouponActivityList = couponClient.listProductSupplierCoupons(param);
		} catch (Exception e1) {
			logger.error("查询商品相关优惠券信息失败 req= {}", JsonUtil.serializer(param), e1);
		}
		return productCouponActivityList;
	}	
	
	

}
